package site.lyun.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import site.lyun.entity.User;
import site.lyun.service.UserService;

import java.util.Map;

public class UserAction extends ActionSupport{
    private User user;
    private UserService userService;

    public UserService getUserService() {
        return userService;
    }
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }


    public String login() throws Exception {
        if(user==null){
            return ERROR;
        }
        User u=userService.find(user);
        if(u!=null){
            if(u.getUserPassword().equals(user.getUserPassword())){
                Map<String,Object> Session = ActionContext.getContext().getSession();
                Session.put("user",user);
                return SUCCESS;
            }
        }
        return ERROR;
    }
    public String register() throws Exception {
        if(user==null){
            return ERROR;
        }
        User u=userService.find(user);
        if(u==null){
            userService.rigister(user);
            return SUCCESS;
        }
        return ERROR;
    }
}
